Kotlin

Constructing Collections

Swift

Constructing from elements

                  val numbersSet = setOf("one", "two", "three", "four")
val emptySet = mutableSetOf<String>()
                
                    let numbersSet = setOf("one", "two", "three", "four")
let emptySet = mutableSetOf<String>()
                  
                  val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key4" to 1)
                
                    let numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key4" to 1)
                  
                  val numbersMap = mutableMapOf<String, String>().apply { this["one"] = "1"; this["two"] = "2" }
                
                    let numbersMap = mutableMapOf<String, String>().apply { this["one"] = "1"; this["two"] = "2" }
                  

Empty collections

                  val empty = emptyList<String>()
                
                    let empty = emptyList<String>()
                  

Initializer functions for lists

                  val doubled = List(3, { it * 2 })  // or MutableList if you want to change its content later
println(doubled)
                
                    let doubled = List(3, { $0 * 2 })  // or MutableList if you want to change its content later
print(doubled)
                  

Concrete type constructors

                  val linkedList = LinkedList<String>(listOf("one", "two", "three"))
val presizedSet = HashSet<Int>(32)
                
                    let linkedList = LinkedList<String>(listOf("one", "two", "three"))
let presizedSet = HashSet<Int>(32)
                  

Copying

                  val sourceList = mutableListOf(1, 2, 3)
val copyList = sourceList.toMutableList()
val readOnlyCopyList = sourceList.toList()
sourceList.add(4)
println("Copy size: ${copyList.size}")   
​
//readOnlyCopyList.add(4)             // compilation error
println("Read-only copy size: ${readOnlyCopyList.size}")
                
                    let sourceList = [1, 2, 3]
let copyList = sourceList.toMutableList()
let readOnlyCopyList = sourceList.toList()
sourceList.add(4)
print("Copy size: ${copyList.size}")   
​
//readOnlyCopyList.add(4)             // compilation error
print("Read-only copy size: ${readOnlyCopyList.size}")
                  
                  val sourceList = mutableListOf(1, 2, 3)    
val copySet = sourceList.toMutableSet()
copySet.add(3)
copySet.add(4)    
println(copySet)
                
                    let sourceList = [1, 2, 3]    
let copySet = sourceList.toMutableSet()
copySet.add(3)
copySet.add(4)    
print(copySet)
                  
                  val sourceList = mutableListOf(1, 2, 3)
val referenceList = sourceList
referenceList.add(4)
println("Source size: ${sourceList.size}")
                
                    let sourceList = [1, 2, 3]
let referenceList = sourceList
referenceList.add(4)
print("Source size: ${sourceList.size}")
                  
                  ​
val sourceList = mutableListOf(1, 2, 3)
val referenceList: List<Int> = sourceList
//referenceList.add(4)            //compilation error
sourceList.add(4)
println(referenceList) // shows the current state of sourceList
                
                    ​
let sourceList = [1, 2, 3]
let referenceList: List<Int> = sourceList
//referenceList.add(4)            //compilation error
sourceList.add(4)
print(referenceList) // shows the current state of sourceList
                  

Invoking functions on other collections

                  ​
val numbers = listOf("one", "two", "three", "four")  
val longerThan3 = numbers.filter { it.length > 3 }
println(longerThan3)
                
                    ​
let numbers = listOf("one", "two", "three", "four")  
let longerThan3 = numbers.filter { it.length > 3 }
print(longerThan3)
                  
                  ​
val numbers = setOf(1, 2, 3)
println(numbers.map { it * 3 })
println(numbers.mapIndexed { idx, value -> value * idx })
                
                    ​
let numbers = setOf(1, 2, 3)
print(numbers.map { $0 * 3 })
print(numbers.mapIndexed { idx, value -> value * idx })
                  
                  val numbers = listOf("one", "two", "three", "four")
println(numbers.associateWith { it.length })
                
                    let numbers = listOf("one", "two", "three", "four")
print(numbers.associateWith { it.length })